package com.itmk.web.room.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.web.room.entity.Room;
import com.itmk.web.room.entity.RoomPage;
import com.itmk.web.room.mapper.RoomMapper;
import com.itmk.web.room.service.RoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmk.web.room_seat.entity.RoomSeat;
import com.itmk.web.room_seat.service.RoomSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author java实战基地
 * @since 2023-10-11
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomSeatService roomSeatService;

    @Transactional
    @Override
    public void add(Room room) {
        //新增阅览室
        int insert = this.baseMapper.insert(room);
        //新增座位号
        if(insert > 0){
            //存储座位号
            List<RoomSeat> list = new ArrayList<>();
            //生成座位号
            for (int i = 1;i<=room.getNumX();i++){
                for (int j = 1;j<=room.getNumY();j++){
                    RoomSeat seat = new RoomSeat();
                    seat.setSeatRow(i);
                    seat.setSeatColumn(j);
                    seat.setRoomId(room.getRoomId());
                    seat.setSeatName(String.valueOf(i)+"排"+String.valueOf(j)+"列");
                    list.add(seat);
                }
            }
            if(list.size() > 0){
                roomSeatService.saveBatch(list);
            }
        }
    }

    @Override
    @Transactional
    public void edit(Room room) {
        //编辑
        int edit = this.baseMapper.updateById(room);
        //查看座位行、列是否发生变化
        if(edit > 0){
            //把原来的座位号删除
            QueryWrapper<RoomSeat> query = new QueryWrapper<>();
            query.lambda().eq(RoomSeat::getRoomId,room.getRoomId());
            roomSeatService.remove(query);
            //重新插入新的座位号
            //存储座位号
            List<RoomSeat> list = new ArrayList<>();
            //生成座位号
            for (int i = 1;i<=room.getNumX();i++){
                for (int j = 1;j<=room.getNumY();j++){
                    RoomSeat seat = new RoomSeat();
                    seat.setSeatRow(i);
                    seat.setSeatColumn(j);
                    seat.setRoomId(room.getRoomId());
                    seat.setSeatName(String.valueOf(i)+"排"+String.valueOf(j)+"列");
                    list.add(seat);
                }
            }
            if(list.size() > 0){
                roomSeatService.saveBatch(list);
            }
        }
    }

    @Override
    @Transactional
    public void delete(Integer roomId) {
        //删除阅览室
        this.baseMapper.deleteById(roomId);
        //删除座位号
         QueryWrapper<RoomSeat> query = new QueryWrapper<>();
         query.lambda().eq(RoomSeat::getRoomId,roomId);
         roomSeatService.remove(query);
    }

    @Override
    public IPage<Room> getList(RoomPage parm) {
        //构造分页对象
        IPage<Room> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());

        return this.baseMapper.getList(page,parm.getCategoryId(),parm.getRoomName());
    }
     @Override
    public IPage<Room> getIndexList(RoomPage parm) {
        //构造分页对象
        IPage<Room> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());

        return this.baseMapper.getIndexList(page,parm.getCategoryId());
    }
}
